import time

from db_init import db
import sqlalchemy as sa
from datetime import datetime
from flask_login import UserMixin


# 用户
class User(db.Model, UserMixin):
    id = sa.Column(sa.Integer, primary_key=True)
    login_time = sa.Column(sa.DateTime, default=datetime.now)
    name = sa.Column(sa.String(16))
    pwd = sa.Column(sa.String(50))
    phone = sa.Column(sa.String(11))
    img = sa.Column(sa.String(1024))
    landlord = sa.Column(sa.Boolean, server_default='0')
    code_id = db.relationship("Code", backref='user')


class House(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    info = sa.Column(sa.String(128))
    zheng = sa.Column(sa.Boolean)
    p_num = sa.Column(sa.Integer)
    c_num = sa.Column(sa.Integer)
    contract_id = db.Column(db.ForeignKey('contract.id'))
    master_id = db.Column(db.ForeignKey('user.id'))
    tenant_id = db.Column(db.ForeignKey('user.id'))
    live = sa.Column(sa.Boolean, default=1)
    city = sa.Column(sa.String(16))
    price = sa.Column(sa.Integer)
    order_id = db.relationship("Order", backref='house')


# 合同
class Contract(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    price = sa.Column(sa.Integer)
    house_id = db.relationship("House", backref='contract')
    tenant_id = db.Column(db.ForeignKey('user.id'))


house_facility = db.Table(
    'house_facility',
    db.Column('house_id', db.ForeignKey('house.id'), primary_key=True),
    db.Column('facility_id', db.ForeignKey('facility.id'), primary_key=True)

)


# 设施
class Facility(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(16))
    icon = sa.Column(sa.String(1024))
    house_id = db.relationship("House", secondary=house_facility, backref='facility')


# 订单
class Order(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    number_id = sa.Column(sa.Integer, unique=True)
    name = sa.Column(sa.String(16))
    house_id = db.Column(db.ForeignKey('house.id'))
    tenant_id = db.Column(db.ForeignKey('user.id'))
    start_t = db.Column(sa.Integer)
    end_t = db.Column(sa.Integer)
    type = sa.Column(sa.Enum(
        '待接单',
        '待支付',
        '完成',
        '已拒绝'
    ), default='待接单')


class Advertise(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    img = sa.Column(sa.String(1024))
    url = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))


class Img(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(16))
    url = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))


class Comment(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    content = sa.Column(sa.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))
    beau = db.Column(db.ForeignKey('comment.id'))
    root_id = db.Column(db.Integer)
    reply = db.relationship("Comment", remote_side=[id], backref='beau_id')
    author_id = db.Column(db.ForeignKey('user.id'))
    num = sa.Column(sa.Integer, default=0)


# 对色对用户
user_role = db.Table(
    'user_role',
    db.Column('user_id', db.ForeignKey('user.id'), primary_key=True),
    db.Column('role_id', db.ForeignKey('role.id'), primary_key=True)
)

# 角色对权限
power_role = db.Table(
    'power_role',
    db.Column('power_id', db.ForeignKey('power.id'), primary_key=True),
    db.Column('role_id', db.ForeignKey('role.id'), primary_key=True)
)


class Role(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(1024))  # 开发 项目经理
    user = db.relationship("User", secondary=user_role, backref='roles', )
    power = db.relationship("Power", secondary=power_role, backref='roles')


class Power(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(1024))  # 查看代码    代码提交
    url = sa.Column(sa.String(1024))  # /code
    method = sa.Column(sa.String(1024))  # get  post


# ########################
class Good(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(1024), server_default='[s')
    open_t = sa.Column(sa.Date, nullable=True)
    open = db.Column(sa.Boolean, default=False)
    price = sa.Column(sa.Integer)
    sale = sa.Column(sa.Integer, default=0)
    code_id = db.relationship("Code", backref='good')


class Code(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    code = sa.Column(sa.String(32))
    # 中奖字段
    open = sa.Column(sa.Boolean)
    good_id = db.Column(db.ForeignKey('good.id'))
    user_id = db.Column(db.ForeignKey('user.id'))


